﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

public partial class inc_image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random rand = new Random();
        int num = rand.Next(1000, 9999);
        string str = num.ToString();

        Bitmap newBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(newBitmap);
        Font font = new Font("Arial", 12);
        SizeF stringSize = g.MeasureString(str, font);
        int nWidth = (int)stringSize.Width;
        int nHeight = (int)stringSize.Height;
        g.Dispose();
        newBitmap.Dispose();
        newBitmap = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
        g = Graphics.FromImage(newBitmap);
        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, nWidth, nHeight));
        g.DrawString(str, font, new SolidBrush(Color.Black), 0, 0);
        MemoryStream tempStream = new MemoryStream();
        newBitmap.Save(tempStream, ImageFormat.Png);


        if (Session.IsNewSession)
            Session.Add("num", str);
        else
        {
            Session["num"] = str;
        }


        Response.ClearContent();
        Response.ContentType = "image/png";
        Response.BinaryWrite(tempStream.ToArray());
        Response.End();
    }
}
